﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    /// <summary>
    /// 判断字符是否英文半角字符或标点
    /// </summary>
    /// <remarks>
    /// 32    空格 
    /// 33-47    标点
    /// 48-57    0~9
    /// 58-64    标点
    /// 65-90    A~Z
    /// 91-96    标点
    /// 97-122    a~z
    /// 123-126  标点
    /// </remarks>
    public static bool IsHalfChar(char c)
    {
        int i = (int)c;
        return i >= 32 && i <= 126;
    }
    /// <summary>
    /// 判断字符是否全角字符或标点
    /// </summary>
    /// <remarks>
    /// <para>全角字符 - 65248 = 半角字符</para>
    /// <para>全角空格例外</para>
    /// </remarks>
    public static bool IsAllChar(char c)
    {
        if (c == '\u3000') return true;

        int i = (int)c - 65248;
        if (i < 32) return false;
        return IsHalfChar((char)i);
    }
    /// <summary>
    /// 将字符串中的全角字符转换为半角
    /// </summary>
    public static string aToh(string s)
    {
        if (s == null || s.Trim() == string.Empty) return s;

        StringBuilder sb = new StringBuilder(s.Length);//拼接字符
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '\u3000')
                sb.Append('\u0020');
            else if (IsAllChar(s[i]))
                sb.Append((char)((int)s[i] - 65248));
            else
                sb.Append(s[i]);
        }

        return sb.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string str = "１２３ａｂｃ＋－*/＝？";
        Response.Write("全角：" + str + "<br/>");
        Response.Write("半角：" + aToh(str));

    }
}
